from langchain.chat_models import init_chat_model
from langchain.output_parsers import ResponseSchema, StructuredOutputParser
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
from langchain_core.runnables import RunnableLambda

model = init_chat_model(model="deepseek-chat",model_provider="deepseek")

#新闻书写chain
news_prompt_template = PromptTemplate.from_template(
    "请根据下面的标题来书写一篇新闻报道：\n{title}"
)
news_chain = news_prompt_template | model

#调蓄信息
def debug_print(x):
    print("中间结果（新闻正文）:", x)
    return x

debug_node = RunnableLambda(debug_print)

#新闻内容解析chain
schemas = [ResponseSchema(name="time", description="新闻发生的时间"),
           ResponseSchema(name="address", description="新闻发生的地点"),
           ResponseSchema(name="event", description="新闻事件的内容")]
parser = StructuredOutputParser.from_response_schemas(schemas)
parseer_prompt_template = PromptTemplate.from_template("请根据以下的新闻内容提取新闻中的信息，并返回JSON格式： \n {news} \n\n {format_instructions}")
chain = parseer_prompt_template.partial(format_instructions=parser.get_format_instructions()) | model | parser

full_chain = news_chain | debug_node | chain
result = full_chain.invoke({"title":"苹果公司在加州发布新款AI芯片"})
print(result)